package com.fendaren.mq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 构建简单模式的生产者，发送消息
 */
public class FooConsumer {

    public static void main(String[] args) throws IOException, TimeoutException {

        // 1.创建连接以及相关的参数配置
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.99.178");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("fendaren");
        factory.setPassword("fendaren");

        // 2.通过工厂创建连接Connection
        Connection connection = factory.newConnection();

        // 3.创建管道Channle
        Channel channel = connection.createChannel();

        // 4.创建队列Queue
        channel.queueDeclare("hello",true,false,false,null);

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(consumerTag);
                System.out.println(envelope.toString());
                System.out.println(properties.toString());
                System.out.println(new String(body));
            }
        };
        channel.basicConsume("hello",true,consumer);
    }
}
